package com.infoauto.opc.gateway.controller;

import com.infoauto.opc.gateway.entity.Result;
import com.infoauto.opc.gateway.entity.ResultCode;
import com.infoauto.opc.gateway.model.gtm4.ControlSignal;
import com.infoauto.opc.gateway.model.gtm4.MonthlyOutput;
import com.infoauto.opc.gateway.model.gtm4.MonthlyOutputExport;
import com.infoauto.opc.gateway.model.gtm4.MonthlyTotal;
import com.infoauto.opc.gateway.service.MonthlyOutputService;
import com.sun.deploy.net.URLEncoder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 月度产量
 */

@CrossOrigin
@RestController
@RequestMapping("/monthlyOutput")
public class MonthlyOutputController {
    @Autowired
    private MonthlyOutputService service;

    /**
     * 上段曲线图
     * @param groupName
     * @param projectName
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value="/list" ,method= RequestMethod.GET)
    public Result selectList(String groupName,String projectName,String startTime,String endTime) {
        try {
            List<MonthlyOutput> list;
            if(startTime.length()>7){
                //按日期查询
                list  = service.selectList(groupName, projectName, startTime, endTime);
            }else{
                //按月份查询
                list = service.selectListByMonth(groupName, projectName, startTime, endTime);
            }
            return new Result(ResultCode.SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下段曲线图
     * @param groupName
     * @param projectName
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value="/list1" ,method= RequestMethod.GET)
    public Result selectList1(String groupName,String projectName,String startTime,String endTime) {
        try {
            List<MonthlyOutput> list;
            if(startTime.length()>7){
                //按日期查询
                list  = service.selectList1(groupName, projectName, startTime, endTime);
            }else{
                //按月份查询
                list = service.selectListByMonth1(groupName, projectName, startTime, endTime);
            }
            return new Result(ResultCode.SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上曲线图累计产量
     * @param groupName
     * @param projectName
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value="/selectTotal" ,method= RequestMethod.GET)
    public Result selectTotal(String groupName,String projectName,String startTime,String endTime) {
        try {
            List<MonthlyTotal> list;
            if(startTime.length()>7){
                //按日期查询
                list  = service.selectTotalByday(groupName, projectName, startTime, endTime);
            }else{
                //按月份查询
                list = service.selectTotalMonth(groupName, projectName, startTime, endTime);
            }
            return new Result(ResultCode.SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下曲线图累计产量
     * @param groupName
     * @param projectName
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value="/selectTotal1" ,method= RequestMethod.GET)
    public Result selectTotal1(String groupName,String projectName,String startTime,String endTime) {
        try {
            List<MonthlyTotal> list;
            if(startTime.length()>7){
                //按日期查询
                list  = service.selectTotalByday1(groupName, projectName, startTime, endTime);
            }else{
                //按月份查询
                list = service.selectTotalMonth1(groupName, projectName, startTime, endTime);
            }
            return new Result(ResultCode.SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(String groupName,String projectName,String startTime,String endTime,HttpServletResponse response) throws Exception {
        //1.构造数据
        List<MonthlyOutputExport> list;
        List<MonthlyOutputExport> list1;
        if(startTime.length()>7){
            //按日期查询
            list = service.selectExportData(groupName,projectName,startTime,endTime);
            list1 = service.selectExportDataForMonth(groupName,projectName,startTime,endTime);
        }else {
            //按月份
            list = service.selectExportData1(groupName,projectName,startTime,endTime);
            list1 = service.selectExportDataForMonth1(groupName,projectName,startTime,endTime);
        }
        //2.创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //3.构造sheet
        String[] titles = {"编号", "组合", "项目","收集日期", "日期(1值)","日期(2值)","日期(3值)","合计(1值+2值+3值)"};
        Sheet sheet = workbook.createSheet("月度统计");
        //创建单元格样式对象
        CellStyle cellStyle = workbook.createCellStyle();
        //设置居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
        cellStyle.setBorderRight(BorderStyle.MEDIUM);//有边框
        sheet.setColumnWidth(1, 20 * 256);//设置第二列的宽度是31个字符宽度
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 20 * 256);
        sheet.setColumnWidth(4, 20 * 256);
        sheet.setColumnWidth(5, 20 * 256);
        sheet.setColumnWidth(6, 20 * 256);
        sheet.setColumnWidth(7, 20 * 256);
        Row row =sheet.createRow(0);
        AtomicInteger headersAi = new AtomicInteger();
        for (String title : titles) {
            Cell cell = row.createCell(headersAi.getAndIncrement());
            cell.setCellValue(title);
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger datasAi = new AtomicInteger(1);
        Cell cell = null;
        int i = 1;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if(list!=null && list.size()>0){
            for (MonthlyOutputExport report : list) {
                Row dataRow = sheet.createRow(datasAi.getAndIncrement());
                //编号
                cell = dataRow.createCell(0);
                cell.setCellValue(i++);
                cell.setCellStyle(cellStyle);
                //组合
                cell = dataRow.createCell(1);
                cell.setCellValue(report.getGroupName());
                cell.setCellStyle(cellStyle);
                //项目名称
                cell = dataRow.createCell(2);
                cell.setCellValue(report.getProjectName());
                cell.setCellStyle(cellStyle);
                //收集日期
                cell = dataRow.createCell(3);
                cell.setCellValue(report.getProductionTime());
                cell.setCellStyle(cellStyle);
                //日期1值
                cell = dataRow.createCell(4);
                cell.setCellValue(report.getShift1Total());
                cell.setCellStyle(cellStyle);
                //日期2值
                cell = dataRow.createCell(5);
                cell.setCellValue(report.getShift2Total());
                cell.setCellStyle(cellStyle);
                //日期3值
                cell = dataRow.createCell(6);
                cell.setCellValue(report.getShift3Total());
                cell.setCellStyle(cellStyle);
                //合计
                cell = dataRow.createCell(7);
                cell.setCellValue(report.getShift1Total()+report.getShift2Total()+report.getShift3Total());
                cell.setCellStyle(cellStyle);

            }
        }
        //3.构造sheet
        String[] titles1 = {"编号", "组合", "项目", "日期(1值)","日期(2值)","日期(3值)","合计(1值+2值+3值)"};
        Row row5 =sheet.createRow(list.size()+3);
        AtomicInteger ai1 = new AtomicInteger();
        for (String title : titles1) {
            cell = row5.createCell(ai1.getAndIncrement());
            cell.setCellValue(title);
            //设置样式
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger ai2 = new AtomicInteger(list.size()+4);
        int j =1;
        if(list1!=null && list1.size()>0){
            for (MonthlyOutputExport report : list1) {
                Row dataRow = sheet.createRow(ai2.getAndIncrement());
                //编号
                cell = dataRow.createCell(0);
                cell.setCellValue(j++);
                cell.setCellStyle(cellStyle);
                //组合
                cell = dataRow.createCell(1);
                cell.setCellValue(report.getGroupName());
                cell.setCellStyle(cellStyle);
                //项目名称
                cell = dataRow.createCell(2);
                cell.setCellValue(report.getProjectName());
                cell.setCellStyle(cellStyle);
                //日期1值
                cell = dataRow.createCell(3);
                cell.setCellValue(report.getShift1Total());
                cell.setCellStyle(cellStyle);
                //日期2值
                cell = dataRow.createCell(4);
                cell.setCellValue(report.getShift2Total());
                cell.setCellStyle(cellStyle);
                //日期3值
                cell = dataRow.createCell(5);
                cell.setCellValue(report.getShift3Total());
                cell.setCellStyle(cellStyle);
                //合计
                cell = dataRow.createCell(6);
                cell.setCellValue(report.getShift1Total()+report.getShift2Total()+report.getShift3Total());
                cell.setCellStyle(cellStyle);

            }
        }
        String fileName = URLEncoder.encode(sheet.getSheetName()+formatter.format(new Date())+".xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new
                String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }



}
